package cn.jluzh.blog.mapper;

import cn.jluzh.blog.modules.category.entity.SysCategory;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysCategoryMapper extends BaseMapper<SysCategory> {

    @Select("select c.*, count(a.category_id) as articles from sys_category c\n" +
            "        left join sys_article a on a.category_id = c.id\n" +
            "        where a.category_id = #{id}")
    SysCategory getCategoryById(Integer id);

    @Select("select c.*, count(a.category_id) as articles from sys_category c\n" +
            "            left join sys_article a on a.category_id = c.id\n" +
            "        group by c.id")
    List<SysCategory> getAllCategory();

    @Select("select * from sys_category where category_name like CONCAT('%',#{query},'%')")
    List<SysCategory> getAllCategoryByName(String query);
}
